#define _CRT_SECURE_NO_WARNINGS
#include"Queue.h"

void QueueInit(Queue* pq)
{
	assert(pq);
	pq->Qhead = NULL;
	pq->Qtail = NULL;
	pq->size = 0;
}

void QueuePush(Queue* pq,QDataType x)
{
	assert(pq);
	QNode* Newnode = (QNode*)malloc(sizeof(QNode));
	if (Newnode == NULL)
	{
		perror("Newnode fail");
		return;
	}
	Newnode->data = x;
	Newnode->next = NULL;
	if (pq->Qtail == NULL)
	{
		pq->Qhead = pq->Qtail = Newnode;
	}
	else
	{
		pq->Qtail->next = Newnode;
		pq->Qtail = Newnode;
	}
	pq->size++;
}

void QueuePop(Queue* pq)
{
	assert(pq);
	assert(pq->Qhead);
	QNode* del = pq->Qhead;
	pq->Qhead = pq->Qhead->next;

	free(del);
	del = NULL;
	if (pq->Qhead == NULL)
	{
		pq->Qtail = NULL;
	}
	pq->size--;
}

QDataType QueueFront(Queue* pq)
{
	assert(pq);
	assert(pq->Qhead);

	return pq->Qhead->data;
}

QDataType QueueTail(Queue* pq)
{
	assert(pq);
	assert(pq->Qtail);

	return pq->Qtail->data;
}

bool QueueEmpty(Queue* pq)
{
	assert(pq);
	
	return pq->Qhead == NULL;
}

int QueueSize(Queue* pq)
{
	assert(pq);
	return pq->size;
}

void QueueDestory(Queue* pq)
{
	assert(pq);
	

	while (pq->size)
	{
		QNode* cur = pq->Qhead;
		pq->Qhead = pq->Qhead->next;
		free(cur);
		cur = NULL;
		pq->size--;
	}

	pq->Qhead = NULL;
	pq->Qtail = NULL;

}